package cz.muni.fi.pa165.mushroom.website.transformerAdapter;


import cz.muni.fi.pa165.mushroom.website.dto.*;
import static cz.muni.fi.pa165.mushroom.website.dto.MushroomTypeDTO.EDIBLE;
import static cz.muni.fi.pa165.mushroom.website.dto.MushroomTypeDTO.NOTEDIBLE;
import static cz.muni.fi.pa165.mushroom.website.dto.MushroomTypeDTO.POISONOUS;
import cz.muni.fi.pa165.mushroom.website.entities.*;

/**
 * @author: Roman Jakubco (rjakubco@redhat.com)
 */
public class Transformer {

    public static GathererDto transformGathererOnGathererDto(Gatherer gatherer){
        if (gatherer == null) {
            throw new IllegalArgumentException("Gatherer parameter is null");
        }

        GathererDto gathererDto = new GathererDto();
        gathererDto.setFirstName(gatherer.getFirstName());
        gathererDto.setId(gatherer.getId());
        gathererDto.setInfo(gatherer.getInfo());
        gathererDto.setLastName(gatherer.getLastName());
        gathererDto.setPassword(gatherer.getPassword());
        gathererDto.setUsername(gatherer.getUsername());
        gathererDto.setSalt(gatherer.getSalt());

       

        return gathererDto;
    }

    public static Gatherer transformGathererDtoOnGatherer(GathererDto gathererDto){
        if (gathererDto == null) {
            throw new IllegalArgumentException("Gatherer parameter is null");
        }

        Gatherer gatherer = new Gatherer();
        gatherer.setFirstName(gathererDto.getFirstName());
        gatherer.setId(gathererDto.getId());
        gatherer.setInfo(gathererDto.getInfo());
        gatherer.setLastName(gathererDto.getLastName());
        gatherer.setPassword(gathererDto.getPassword());
        gatherer.setUsername(gathererDto.getUsername());
        gatherer.setSalt(gathererDto.getSalt());
        

        return gatherer;
    }

    public static MushroomDto transformMushroomOnMushroomDto(Mushroom mushroom){
        if(mushroom == null){
            throw new IllegalArgumentException("Mushroom parameter is null");
        }
        MushroomDto mushroomDto = new MushroomDto();
        mushroomDto.setId(mushroom.getMushroomId());
        mushroomDto.setName(mushroom.getName());
        switch (mushroom.getType()){
            case EDIBLE: mushroomDto.setType(MushroomTypeDTO.EDIBLE); break;
            case NOTEDIBLE: mushroomDto.setType(MushroomTypeDTO.NOTEDIBLE); break;
            case POISONOUS: mushroomDto.setType(MushroomTypeDTO.POISONOUS);
        }

        mushroomDto.setGrowsFrom(mushroom.getGrowsFrom());
        mushroomDto.setGrowsUntil(mushroom.getGrowsUntil());
        
        return mushroomDto;
    }

    public static Mushroom transformMushroomDtoOnMushroom(MushroomDto mushroomDto){
        if(mushroomDto == null){
            throw new IllegalArgumentException("Mushroom parameter is null");
        }
        Mushroom mushroom = new Mushroom();
        mushroom.setMushroomId(mushroomDto.getId());
        mushroom.setName(mushroomDto.getName());
        if( !( mushroomDto.getType() == null )){
           switch (mushroomDto.getType()){
            case EDIBLE: mushroom.setType(MushroomType.EDIBLE); break;
            case NOTEDIBLE: mushroom.setType(MushroomType.NOTEDIBLE); break;
            case POISONOUS: mushroom.setType(MushroomType.POISONOUS);
            } 
        } else {
           mushroom.setType(MushroomType.EDIBLE); 
        }
        
        
        mushroom.setGrowsFrom(mushroomDto.getGrowsFrom());
        mushroom.setGrowsUntil(mushroomDto.getGrowsUntil());
        
        return mushroom;
    }

    public static VisitDto transformVisitOnVisitDto(Visit visit){
        if(visit == null){
            throw new IllegalArgumentException("Visit parameter is null");            
        }
        VisitDto visitDto = new VisitDto();
        visitDto.setId(visit.getId());
        visitDto.setInfo(visit.getInfo());
        visitDto.setForestDto(transformForestOnForestDto(visit.getForest()));              
        visitDto.setGathererDto(transformGathererOnGathererDto(visit.getGatherer()));        
        if(visit.getMushrooms() != null){
          for(Mushroom m : visit.getMushrooms()){
            visitDto.getMushrooms().add(transformMushroomOnMushroomDto(m));
        }  
        }
        
        
        return visitDto;
    }

    public static Visit transformVisitDtoOnVisit(VisitDto visitDto){
        if(visitDto == null){
            throw new IllegalArgumentException("VisitDto parameter is null");            
        }
        Visit visit = new Visit();
        visit.setId(visitDto.getId());
        visit.setInfo(visitDto.getInfo());        
        visit.setForest(transformForestDtoOnForest(visitDto.getForestDto()));
        visit.setGatherer(transformGathererDtoOnGatherer(visitDto.getGathererDto()));
        if(visitDto.getMushrooms() != null){
          for(MushroomDto m : visitDto.getMushrooms()){
            visit.getMushrooms().add(transformMushroomDtoOnMushroom(m));
        }  
        }
        
        
        return visit;
    }

    public static ForestDto transformForestOnForestDto(Forest forest){
        if (forest == null) {
            throw new IllegalArgumentException("Forest parameter is null");
        }
        
        ForestDto forestDto = new ForestDto();
        forestDto.setName(forest.getName());
        forestDto.setId(forest.getId());
        forestDto.setDescription(forest.getDescription()); 
        if(forest.getVisits() != null){
            for(Visit v : forest.getVisits()){
                forestDto.getVisits().add(v.getId());
            }
        }
        
        
        return forestDto;
    }

    public static Forest transformForestDtoOnForest(ForestDto forestDto){
        if (forestDto == null) {
            throw new IllegalArgumentException("Forest parameter is null");
        }
        
        Forest forest = new Forest();
        
        forest.setId(forestDto.getId());
        forest.setName(forestDto.getName());
        forest.setDescription(forestDto.getDescription());       
        
        return forest;
    }    
 
}
